﻿Public Class frmReglerFacture
    Public pCd_cli As Integer
    Public pNo_fact As Integer
    Public pTotalTTC As Decimal

    Private szSQL As String
    Private rs As ADODB.Recordset

    Private Sub frmReglerFacture_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Me.Text = "Régler la facture : " & pNo_fact
        Call InitVersements(0)
        Call InitDossiers(0)

    End Sub

    Private Sub InitVersements(ByVal ix As Integer)

        Dim nb As Integer = 0

        ' effacer la listview
        LV_VERSEMENT.Items.Clear()

        ' lire toutes les réglements de la facture
        szSQL = "CALL spGET_LIV_BNQ('byDISPO','" & gSocieteCode & "'," & pCd_cli & ",0,'','')"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)
        Do While Not rs.EOF
            LV_VERSEMENT.Items.Add(rs.Fields("date").Value)
            LV_VERSEMENT.Items(nb).SubItems.Add(LibByCode("P_MODEREG", rs.Fields("modereg").Value))
            LV_VERSEMENT.Items(nb).SubItems.Add(rs.Fields("ets").Value)
            LV_VERSEMENT.Items(nb).SubItems.Add(rs.Fields("jst").Value)
            LV_VERSEMENT.Items(nb).SubItems.Add(rs.Fields("montantregl").Value)
            LV_VERSEMENT.Items(nb).SubItems.Add(rs.Fields("montantregl").Value - rs.Fields("montantdebit").Value)
            LV_VERSEMENT.Items(nb).SubItems.Add(Format(rs.Fields("cod_bnq").Value, "000000"))
            LV_VERSEMENT.Items(nb).SubItems.Add(rs.Fields("modereg").Value)
            rs.MoveNext()
            nb = nb + 1
        Loop
        rs.Close()

        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_VERSEMENT)

    End Sub

    Private Sub InitDossiers(ByVal ix As Integer)

        Dim nb As Integer = 0
        Dim mtregle As Decimal

        ' effacer la listview
        LV_DOSSIERS.Items.Clear()

        ' lire toutes les réglements de la facture
        szSQL = "CALL spGET_FACTURE_REG('" & gSocieteCode & "'," & pNo_fact & ",0,0)"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)
        Do While Not rs.EOF
            LV_DOSSIERS.Items.Add(rs.Fields("date").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(LibByCode("P_MODEREG", rs.Fields("modereg").Value))
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("ets").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("jst").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("montant").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(Format(rs.Fields("cod_bnq").Value, "000000"))
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("modereg").Value)
            mtregle += rs.Fields("montant").Value
            rs.MoveNext()
            nb = nb + 1
        Loop
        rs.Close()

        TE_TOTALTTC.Text = pTotalTTC.ToString("C")
        TE_DEJAREGLE.Text = mtregle.ToString("C")
        TE_RESTEREGLE.Text = pTotalTTC - mtregle

        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_DOSSIERS)

    End Sub

    Private Sub BO_ADD_LIGNE_Click(sender As System.Object, e As System.EventArgs) Handles BO_ADD_LIGNE.Click

        If IsUserAuthorised(ctCompta) = False Then
            Exit Sub
        End If
        frmReglerMAJ.pCodeBanque = 0
        frmReglerMAJ.pCodeClient = pCd_cli
        frmReglerMAJ.ShowDialog()
        Call InitVersements(0)

    End Sub

    Private Sub LV_VERSEMENT_DoubleClick(sender As Object, e As System.EventArgs) Handles LV_VERSEMENT.DoubleClick

        frmReglerMAJ.pCodeBanque = LV_VERSEMENT.SelectedItems(0).SubItems(6).Text
        frmReglerMAJ.pCodeClient = pCd_cli
        frmReglerMAJ.ShowDialog()
        Call InitVersements(LV_VERSEMENT.SelectedItems(0).Index)

    End Sub

    Private Sub LV_VERSEMENT_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles LV_VERSEMENT.DragDrop

        Dim ModeRegl As Integer = 0
        'Dim myItems() As ListViewItem = e.Data.GetData("System.Windows.Forms.ListViewItem()")

        'For Each myItem As ListViewItem In myItems
        '    ' Add the item to the target list.  
        '    Dim lvItem As ListViewItem = sender.Items.Add(myItem.Text)

        '    'Loop through the sub items AND add it to the destination Listview  
        '    Dim j As Integer
        '    For j = 1 To myItem.SubItems.Count - 1
        '        lvItem.SubItems.Add(myItem.SubItems(j).Text)
        '    Next
        '    i += 1
        'Next

        If LV_DOSSIERS.SelectedItems.Count = 0 Then
            Exit Sub
        End If

        ' supprimer dans FACTURE_REG
        szSQL = "CALL spDEL_FACTURE_REG('" & gSocieteCode & "'," & pNo_fact & "," & LV_DOSSIERS.SelectedItems.Item(0).SubItems(5).Text & ")"
        ExecQuery(ctDSN_DEVFAC, szSQL)

        ModeRegl = LV_DOSSIERS.SelectedItems.Item(0).SubItems(6).Text
        ' si avoir ou facture
        If ModeRegl = 7 Or ModeRegl = 8 Then
            ' supprimer une ligne de réglement pour cette facture
            szSQL = "CALL spDEL_FACTURE_REG('" & gSocieteCode & "'," & CInt(LV_DOSSIERS.SelectedItems.Item(0).SubItems(3).Text) & "," & ModeRegl * 1000000 + pNo_fact & ")"
            ExecQuery(ctDSN_DEVFAC, szSQL)
        End If
        ' supprimer la ligne sélectionnée de LV_DOSSIERS 
        LV_DOSSIERS.Items.Remove(LV_DOSSIERS.SelectedItems.Item(0))

        Call InitVersements(0)

    End Sub

    Private Sub LV_VERSEMENT_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles LV_VERSEMENT.DragEnter
        ' Check for the custom DataFormat ListViewItem array.  
        If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem()") Then
            e.Effect = DragDropEffects.Move
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub LV_VERSEMENT_ItemDrag(sender As Object, e As System.Windows.Forms.ItemDragEventArgs) Handles LV_VERSEMENT.ItemDrag
        Dim myItems(sender.SelectedItems.Count - 1) As ListViewItem
        Dim i As Integer = 0

        ' Loop though the SelectedItems collection for the source.  
        For Each myItem As ListViewItem In sender.SelectedItems
            ' Add the ListViewItem to the array of ListViewItems.  
            myItems(i) = myItem
            i = i + 1
        Next
        ' Create a DataObject containg the array of ListViewItems.  
        sender.DoDragDrop(New DataObject("System.Windows.Forms.ListViewItem()", myItems), DragDropEffects.Move)
    End Sub

    Private Sub LV_VERSEMENT_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles LV_VERSEMENT.KeyDown
        Call LV_VERSEMENT_DoubleClick(sender, e)
    End Sub

    Private Sub LV_DOSSIERS_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles LV_DOSSIERS.DragDrop
        Dim ModeRegl As Integer = 0
        Dim CodeBnq As Integer = 0
        Dim Banque As String
        Dim NoPiece As String
        Dim szDate As String
        Dim mtVersement As Decimal
        Dim mtReglement As Decimal

        If LV_VERSEMENT.SelectedItems.Count = 0 Then
            Exit Sub
        End If
        ' reste à payer ? non, quitter
        If CDec(TE_RESTEREGLE.Text) <= 0 Then
            Exit Sub
        End If

        ModeRegl = LV_VERSEMENT.SelectedItems.Item(0).SubItems(7).Text
        CodeBnq = LV_VERSEMENT.SelectedItems.Item(0).SubItems(6).Text
        szDate = LV_VERSEMENT.SelectedItems.Item(0).SubItems(0).Text
        Banque = LV_VERSEMENT.SelectedItems.Item(0).SubItems(2).Text
        NoPiece = LV_VERSEMENT.SelectedItems.Item(0).SubItems(3).Text
        mtVersement = LV_VERSEMENT.SelectedItems.Item(0).SubItems(4).Text
        If CDec(TE_RESTEREGLE.Text) >= mtVersement Then
            mtReglement = mtVersement
        Else
            mtReglement = CDec(TE_RESTEREGLE.Text)
        End If

        ' créer une ligne pour régler la facture
        szSQL = "CALL spINS_FACTURE_REG('" & gSocieteCode & "'," & pNo_fact & "," & ModeRegl & "," & CodeBnq & "," & pCd_cli & ",'" & _
        Banque & "','" & NoPiece & "','" & Format(CDate(szDate), "yyyy/MM/dd") & "'," & mtReglement & ")"
        ExecQuery(ctDSN_DEVFAC, szSQL)

        If ModeRegl = 7 Or ModeRegl = 8 Then
            If ModeRegl < 7 Then
                Banque = "_AVR"       ' mode Avoir
            Else
                Banque = "_FCT"       ' mode facture
            End If

            ' créer une ligne pour régler l'avoir
            szSQL = "CALL spINS_FACTURE_REG('" & gSocieteCode & "'," & CLng(NoPiece) & "," & ModeRegl & "," & ModeRegl * 1000000 + pNo_fact & "," & pCd_cli & ",'" & Banque & "','" & _
            Format(pNo_fact, "000000") & "','" & Format(CDate(szDate), "yyyy-MM-dd") & "'," & mtReglement * -1 & ");"
            ExecQuery(ctDSN_DEVFAC, szSQL)
        End If

        ' redessiner la listview LV_DOSSIERS
        Call InitVersements(0)
        Call InitDossiers(0)

    End Sub

    Private Sub LV_DOSSIERS_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles LV_DOSSIERS.DragEnter
        ' Check for the custom DataFormat ListViewItem array.  
        If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem()") Then
            e.Effect = DragDropEffects.Move
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub


    Private Sub LV_DOSSIERS_ItemDrag(sender As Object, e As System.Windows.Forms.ItemDragEventArgs) Handles LV_DOSSIERS.ItemDrag
        Dim myItems(sender.SelectedItems.Count - 1) As ListViewItem
        Dim i As Integer = 0

        ' Loop though the SelectedItems collection for the source.  
        For Each myItem As ListViewItem In sender.SelectedItems
            ' Add the ListViewItem to the array of ListViewItems.  
            myItems(i) = myItem
            i = i + 1
        Next
        ' Create a DataObject containg the array of ListViewItems.  
        sender.DoDragDrop(New DataObject("System.Windows.Forms.ListViewItem()", myItems), DragDropEffects.Move)
    End Sub
End Class